# This file creates Table SI.2

# install.packages("xtable")
# install.packages("ggplot2")
# install.packages("dplyr")

library(xtable)
library(ggplot2)
library(dplyr)

# read output tables 
outtab_mom <- read.table("mother_firsttwo_res.txt", 
                         header = FALSE, skip = 1)
                        
outtab_dad <- read.table("father_firsttwo_res.txt", 
                         header = FALSE, skip = 1)

f_mom <- read.table("f_stat_mothers_firsttwo.txt")
f_dad <- read.table("f_stat_fathers_firsttwo.txt")

# set up table for mothers 
main_tab_mom <- 
  matrix(NA, nrow = 9, ncol = 3)

colnames(main_tab_mom) <-
  c("2009 Local", "2013 Local", "2014 European")

rownames(main_tab_mom) <- 
  c("Two first children",
    "same sex",
    "3",
    "No. of children (2sls)",
    "5",
    "6",
    "Turnout if two first children opposite sex",
    "N")

for(i in 1:3){
  main_tab_mom[1, i] <- round(outtab_mom[4*i - 1, 2], 3)
  main_tab_mom[2, i] <- paste("(",round(outtab_mom[4*i - 1, 3], 3),")", sep ="")
  main_tab_mom[3, i] <- paste("[",
                                    round(outtab_mom[4*i - 1, 3]*qnorm(0.025) + outtab_mom[4*i - 1, 2], 3),
                                    "; ",
                                    round(outtab_mom[4*i - 1, 3]*qnorm(0.975) + outtab_mom[4*i - 1, 2], 3),
                                    "]", sep ="")
  main_tab_mom[4, i] <- round(outtab_mom[4*i, 2], 3)
  main_tab_mom[5, i] <- paste("(",round(outtab_mom[4*i, 3], 3),")", sep ="")
  main_tab_mom[6, i] <- paste("[",
                              round(outtab_mom[4*i, 3]*qnorm(0.025) + outtab_mom[4*i, 2], 3),
                              "; ",
                              round(outtab_mom[4*i, 3]*qnorm(0.975) + outtab_mom[4*i, 2], 3),
                              "]", sep ="")
  main_tab_mom[7, i] <- round(outtab_mom[4*i - 3, 2], 3)
  main_tab_mom[8, i] <- sum(outtab_mom[c(4*i - 3, 4*i - 2), 6])
  main_tab_mom[9, i] <- round(f_mom[i, 3], 1)
}

# Set table up for fathers
main_tab_dad <- 
  matrix(NA, nrow = 9, ncol = 3)

colnames(main_tab_dad) <-
  c("2009 Local", "2013 Local", "2014 European")

rownames(main_tab_dad) <- 
  c("Two first children",
    "same sex",
    "3",
    "No. of children (2sls)",
    "5",
    "6",
    "Turnout if two first children opposite sex",
    "N")

for(i in 1:3){
  main_tab_dad[1, i] <- round(outtab_dad[4*i - 1, 2], 3)
  main_tab_dad[2, i] <- paste("(",round(outtab_dad[4*i - 1, 3], 3),")", sep ="")
  main_tab_dad[3, i] <- paste("[",
                              round(outtab_dad[4*i - 1, 3]*qnorm(0.025) + outtab_dad[4*i - 1, 2], 3),
                              "; ",
                              round(outtab_dad[4*i - 1, 3]*qnorm(0.975) + outtab_dad[4*i - 1, 2], 3),
                              "]", sep ="")
  main_tab_dad[4, i] <- round(outtab_dad[4*i, 2], 3)
  main_tab_dad[5, i] <- paste("(",round(outtab_dad[4*i, 3], 3),")", sep ="")
  main_tab_dad[6, i] <- paste("[",
                              round(outtab_dad[4*i, 3]*qnorm(0.025) + outtab_dad[4*i, 2], 3),
                              "; ",
                              round(outtab_dad[4*i, 3]*qnorm(0.975) + outtab_dad[4*i, 2], 3),
                              "]", sep ="")
  main_tab_dad[7, i] <- round(outtab_dad[4*i - 3, 2], 3)
  main_tab_dad[8, i] <- sum(outtab_dad[c(4*i - 3, 4*i - 2), 6])
  main_tab_dad[9, i] <- round(f_dad[i, 3], 1)
}

## Effects in table
xtable(main_tab_mom)
xtable(main_tab_dad)